#pragma once

#include <stdint.h>
#include <map>

#include "Socket.h"
#include "SocketDefine.h"

namespace nio {

	enum EventSet
	{
		EventSetRead = 1 << 1,
		EventSetWrite = 1 << 2,
		EventSetExcept = 1 << 3,
	};

	class Selectable {
	public:
		virtual SOCKET getFD() = 0;
		//EventSet
		virtual void onEvents(int events) = 0;
	};

	class Selector
	{
	private:
		//events map<fd, events>
		std::map<SOCKET, int> eventsMap;
		//events map<fd, Selectable>
		std::map<SOCKET, Selectable*> selectableMap;
		Socket* wakener = nullptr;
	public:
		Selector();
		virtual ~Selector();
		void wakeup();
		void waitForEvents(int64_t timeout);

		//listen events
		void addEvents(Selectable* selectable, int events);
		void removeEvents(Selectable* selectable, int events);
		void clearEvents(Selectable* selectable);
	};

}